package com.hjf.booking.constants;

/**
 * @author elton
 * @version V1.0.0
 * @title 统一响应输出对象
 * @date 2017-11-30 15:32:39
 * @description
 **/
public class BaseResponse<T> {
    private Integer code;
    private String message;
    private Integer messsageCode;
    private T data;

    public Integer getCode() {
        return code;
    }

    public final class StatusCode {
        public static final int SUCCESS = 200;//处理正常
        public static final int CREATED = 201;//资源创建成功
        public static final int DELETED = 204;//资源删除成功
        public static final int AUTH_ERROR = 401;//操作未授权或授权失败
        public static final int SERVER_ERROR = 500;//服务器内部出错
        public static final int BAD_PARAMS_REQUEST = 400;//调用不合法，确切的错误应该在error payload中描述，请求参数有问题
        public static final int FORBIDDEN = 403;//服务端正常解析和请求，但是调用被回绝或者不被允许.禁止操作，比如业务当前的状态不允许提交的操作
    }

    public static BaseResponse ok() {
        BaseResponse r = new BaseResponse();
        r.setCode(BaseResponse.StatusCode.SUCCESS);
        r.setMessage(RspMessage.BizCommon.SUCCESS);
        return r;
    }

    public static BaseResponse forbidden(String message) {
        BaseResponse r = new BaseResponse();
        r.setCode(BaseResponse.StatusCode.FORBIDDEN);
        r.setMessage(message);
        return r;
    }

    public static BaseResponse forbidden(String message, int msgCode) {
        BaseResponse r = new BaseResponse();
        r.setCode(BaseResponse.StatusCode.FORBIDDEN);
        r.setMesssageCode(msgCode);
        r.setMessage(message);
        return r;
    }

    public static <T> BaseResponse<T> forbidden(String message, int msgCode, T t) {
        BaseResponse r = new BaseResponse();
        r.setCode(BaseResponse.StatusCode.FORBIDDEN);
        r.setMesssageCode(msgCode);
        r.setMessage(message);
        r.setData(t);
        return r;
    }

    public static BaseResponse authError(String message) {
        BaseResponse r = new BaseResponse();
        r.setCode(BaseResponse.StatusCode.AUTH_ERROR);
        r.setMessage(message);
        return r;
    }

    public static <T> BaseResponse<T> ok(T t) {
        BaseResponse<T> r = new BaseResponse<T>();
        r.setCode(BaseResponse.StatusCode.SUCCESS);
        r.setMessage(RspMessage.BizCommon.SUCCESS);
        r.setData(t);
        return r;
    }


    public static <T> BaseResponse<T> ok(T t, String message) {
        BaseResponse<T> r = new BaseResponse<T>();
        r.setCode(BaseResponse.StatusCode.SUCCESS);
        r.setData(t);
        r.setMessage(message);
        return r;
    }

    public static BaseResponse okWithNotifyMsg(String msg) {
        BaseResponse r = new BaseResponse();
        r.setCode(BaseResponse.StatusCode.SUCCESS);
        r.setMessage(msg);
        return r;
    }

//    public static <T> BaseResponse<T> ok(T t,String message,int msgCode) {
//        BaseResponse<T> r = new BaseResponse<T>();
//        r.setCode(BaseResponse.StatusCode.SUCCESS);
//        r.setData(t);
//        r.setMesssageCode(msgCode);
//        r.setMessage(message);
//        return r;
//    }


    public static <T> BaseResponse<T> bad(String message) {
        BaseResponse<T> e = new BaseResponse<T>();
        e.setCode(BaseResponse.StatusCode.BAD_PARAMS_REQUEST);
        e.setMessage(message);
        return e;
    }

    public static <T> BaseResponse<T> bad(String message, int messsageCode) {
        BaseResponse<T> e = new BaseResponse<T>();
        e.setCode(BaseResponse.StatusCode.BAD_PARAMS_REQUEST);
        e.setMesssageCode(messsageCode);
        e.setMessage(message);
        return e;
    }

    public static <T> BaseResponse<T> bad(T t, String message) {
        BaseResponse<T> e = new BaseResponse<T>();
        e.setCode(BaseResponse.StatusCode.BAD_PARAMS_REQUEST);
        e.setData(t);
        e.setMessage(message);
        return e;
    }

    public static <T> BaseResponse<T> error(String message, int messsageCode) {
        BaseResponse<T> e = new BaseResponse<T>();
        e.setCode(BaseResponse.StatusCode.SERVER_ERROR);
        e.setMesssageCode(messsageCode);
        e.setMessage(message);
        return e;
    }

    public static <T> BaseResponse<T> error() {
        BaseResponse<T> e = new BaseResponse<T>();
        e.setCode(BaseResponse.StatusCode.SERVER_ERROR);
        e.setMessage(RspMessage.BizCommon.SERVER_ERROR);
        return e;
    }

    public static <T> BaseResponse<T> error(int code) {
        BaseResponse<T> e = new BaseResponse<T>();
        e.setCode(code);
        e.setMessage(RspMessage.BizCommon.SERVER_ERROR);
        return e;
    }

    public Integer getMesssageCode() {
        return messsageCode;
    }

    public void setMesssageCode(Integer messsageCode) {
        this.messsageCode = messsageCode;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
